package chapter10.dao;


import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class UsersDaoTest {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, ParseException {
        Connection conn = null;
        try {
            conn = DbUtils.getConnection();
            UsersDao usersDao = new UsersDao(conn);

            usersDao.getAll();

            System.out.println();
            System.out.println("==================== test insert ====================");
            User insertRecord = new User(null, "zjl", "zjlpasswd", "zhujiale@niit.edu.cn", new Date(parseDate("1985-03-30").getTime()));
            int id = usersDao.insert(insertRecord);
            usersDao.getById(id);

            System.out.println();
            System.out.println("==================== test update ====================");
            User updateRecord = new User(id, "zjl2", "zjlpasswd2", "zhujiale2@niit.edu.cn", new Date(parseDate("1986-03-30").getTime()));
            usersDao.update(updateRecord);
            usersDao.getById(id);

            System.out.println();
            System.out.println("==================== test delete ====================");
            usersDao.delete(id);
            usersDao.getAll();
        } finally {
            IOUtils.close(conn);
        }

    }

    private static java.util.Date parseDate(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(dateString);
    }

}
